<?php

namespace app\common\model;

use think\Db;

class UserMemberAccountModel extends BaseModel
{

    protected $name = 'user_member_account'; //定义用户表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = false;
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = []; //插入
    protected $update = []; //修改
    //类型自动转换
    protected $type = [];

    /*读取器*/
    protected function getChangeTypeTextAttr($value,$data){
        $arr = $this->setChangeType();
        return $arr[$data['change_type']];
    }

    /**
     * 设置支付类型
     * @return array
     */
    public function setChangeType(){
        $arr = [1=>'充值',2=>'提现',3=>'提交订单',4=>'订单支付',5=>'订单完成',6=>'订单退款',7=>'关闭订单',8=>'删除订单',99=>'人工调节'];
        return $arr;
    }
    /**
     * 记录帐户变动信息
     * @param $siteId
     * @param $systemUid
     * @param $memberUid
     * @param $orderId
     * @param array $account['user_money'=>1,'prepare_money'=>2,'pay_points'=>3,'prepare_points'=>4] 需要计算得放入数组即可
     * @param $changeType
     * @param string $changeDesc
     */
    public function changeAccount($siteId, $systemUid, $memberUid, $orderId, $account = [], $changeType, $changeDesc = '')
    {
        $systemUsername = '';
        if ($systemUid > 0) {
            $systemUsername = get_field_value('user', 'username', ['uid' => $systemUid]);
        }
        $memberUsername = get_field_value('user', 'username', ['uid' => $memberUid]);
        $memberInfo     = Db::table('user_member')->where('uid', $memberUid)->find();
        if(empty($changeDesc)){
            $arr = $this->setChangeType();
            $changeDesc = $arr['$changeType'];
        }
        /* 插入帐户变动记录 */
        $accountData = [
            'site_id'         => $siteId,
            'system_uid'      => $systemUid,
            'system_username' => $systemUsername,
            'member_uid'      => $memberUid,
            'member_username' => $memberUsername,
            'order_id'        => $orderId,
            'change_type'     => $changeType,
            'change_time'     => time(),
            'change_desc'     => $changeDesc,
        ];
        if (array_key_exists('user_money', $account)) {
            $accountData['user_money']     = $account['user_money'];
            $accountData['user_money_log'] = $memberData['user_money'] = $memberInfo['user_money'] + $account['user_money'];
            if($memberData['user_money']<0){
                $this->error = '用户可用金额不足！';
                return false;
            }
        }
        if (array_key_exists('prepare_money', $account)) {
            $accountData['prepare_money']     = $account['prepare_money'];
            $accountData['prepare_money_log'] = $memberData['prepare_money'] = $memberInfo['prepare_money'] + $account['prepare_money'];
            if($memberData['prepare_money']<0){
                $this->error = '用户预存金额不足！';
                return false;
            }
        }
        if (array_key_exists('pay_points', $account)) {
            $accountData['pay_points']     = $account['pay_points'];
            $accountData['pay_points_log'] = $memberData['pay_points'] = $memberInfo['pay_points'] + $account['pay_points'];
            if($memberData['pay_points']<0){
                $this->error = '用户积分不足！';
                return false;
            }
        }
        if (array_key_exists('prepare_points', $account)) {
            $accountData['prepare_points']     = $account['prepare_points'];
            $accountData['prepare_points_log'] = $memberData['prepare_points'] = $memberInfo['prepare_points'] + $account['prepare_points'];
            if($memberData['prepare_points']<0){
                $this->error = '用户预存积分不足！';
                return false;
            }
        }
        if(!empty($memberData)){
            Db::startTrans();
            try{
                Db::table('user_member_account')->insert($accountData);
                Db::table('user_member')->where('uid',$memberUid)->update($memberData);
                Db::commit();
                return true;
            } catch (\Exception $e) {
                Db::rollback();
                $this->error = '执行失败';
                return false;
            }
        }
        $this->error = '没有要更新得数据';
        return false;
    }

}